<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/16
 * Time: 15:52
 */

namespace Admin\Controller;
use Think\Controller;

class MarketController extends Controller{
    /**
     * 服务市场
     */
    public function service_market(){
        $info=M('service_market','','XYC_SERVICE');
        $where['status']=1;
        $where['type']=1;
        $filed='id,name,logo,cooperation,settlement,cycle,effect,avg_price';
        $order='ctime desc';
        $limit=5;
        $data['advertisement']=$info->where($where)->limit($limit)->field($filed)->order($order)->select();
        $where['type']=2;
        $data['loan']=$info->where($where)->field($filed)->limit($limit)->order($order)->select();
        $this->data=$data;
        $this->display();
    }
    /**
     * 服务市场详情
     */
    public function market_detail(){
        $this->status=M('service_binding','','XYC_SERVICE')->order('id desc')->where(['server_id'=>I('id'),'shop_identifier'=>$_SESSION['shop_identifier']])->getfield('status');
        $this->data=M('service_market','','XYC_SERVICE')->where(['id'=>I('id')])->field('ctime,status,type',true)->find();
        $this->display();
    }

    /**
     * 申请服务商
     */
    public function apply(){
        $info=M('service_binding','','XYC_SERVICE');
        $id=I('id');
        $name=I('name');
        if(empty($id) || empty($name)){
            MycommonController::ajax('1002','参数错误');
        }
        $status=$info->where(['server_id'=>$id,'shop_identifier'=>$_SESSION['shop_identifier']])->order('id desc')->getField('status');
        if($status==2){
            MycommonController::ajax('1002','已成功接入，请勿重新申请');
        }elseif ($status==1){
            MycommonController::ajax('1002','服务商正在审核中，请勿重新申请');
        }
        $data['server_id']=$id;
        $data['shop_identifier']=$_SESSION['shop_identifier'];
        $data['proposer']=$_SESSION['name'];
        $data['phone']=$_SESSION['mobile'];
        $data['ctime']=time();
        $data['status']=1;
        if($info->add($data)){
            add_control_log($_SESSION['shop_identifier'],$_SESSION['name'],'在【商户后台-服务市场】申请接入服务商【'.$name.'】',3);
            MycommonController::ajax('1001','提交成功');
        }else{
            MycommonController::ajax('1002','提交失败');
        }
    }
}